package com.kindnesslibrary.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;

import com.kindnesslibrary.R;
import com.kindnesslibrary.base.BaseActivity;
import com.kindnesslibrary.base.BaseApplication;
import com.kindnesslibrary.util.Contants;
import com.kindnesslibrary.util.ToastUtil;
import com.kindnesslibrary.ximage.XImageLoader;
import com.kindnesslibrary.ximage.XImgSelConfig;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.ArrayList;

import static com.kindnesslibrary.util.Contants.CAMERAFILE;

/**
 * 选择图片
 */
public class ChooseImageActivity extends BaseActivity {

    View rl_choose_image_choose, rl_choose_image_take, rl_choose_image_cancle;
    private File photoPath, outPath;
    private XImgSelConfig xImgSelConfig;

    XImageLoader xImageLoader = (context, localPath, iv, adapter, index) -> {
        ImageLoader.getInstance().displayImage("file://" + localPath, iv, BaseApplication.getInstance().options, null);
    };

    @Override
    protected void handlerMessage(Message msg) {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_choose_image_layout;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        // 获取缩略图图片列表
        rl_choose_image_choose = findViewById(R.id.rl_choose_image_choose);
        rl_choose_image_take = findViewById(R.id.rl_choose_image_take);
        rl_choose_image_cancle = findViewById(R.id.rl_choose_image_cancle);
        xImgSelConfig = (XImgSelConfig) getIntent().getSerializableExtra(Contants.XIMAGEKEY);
    }

    @Override
    protected void initEvent() {
        rl_choose_image_choose.setOnClickListener(view -> {
            if(xImgSelConfig == null) {
                xImgSelConfig = new XImgSelConfig.Builder(xImageLoader).maxNum(9).showCamera(true).isCrop(true).isPreview().build();
                XImageActivity.open(this, xImgSelConfig, Contants.ALBUM_CONTENT_DATA_CODE);
            }else{
                XImageActivity.open(this, xImgSelConfig.setImageLoader(xImageLoader), Contants.ALBUM_CONTENT_DATA_CODE);
            }
        });
        rl_choose_image_take.setOnClickListener(view -> {
            photoPath = startCamera(Contants.CAMERA_CODE_RESULT);
        });
        rl_choose_image_cancle.setOnClickListener(view -> {
            finish();
        });
    }

    @Override
    protected int getLayout() {
        return 0;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == Contants.CAMERA_CODE_RESULT) {
                if (photoPath != null && photoPath.exists()) {
                    if(xImgSelConfig != null && xImgSelConfig.needCrop){
                        String sf = new StringBuffer(photoPath.getAbsolutePath()).insert(photoPath.getAbsolutePath().lastIndexOf("."), System.currentTimeMillis()).toString();
                        outPath = new File(sf);
                        startPhotoZoom(Contants.CAMERA_CODE_ZOOM, photoPath, outPath);
                    }else{
                        Intent intent = new Intent();
                        intent.putExtra(Contants.TYPE, Contants.CAMERA);
                        intent.putExtra(Contants.CAMERAFILE, photoPath.getAbsolutePath());
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }else{
                    ToastUtil.showToast("图片不存在");
                }
            } else if (requestCode == Contants.CAMERA_CODE_ZOOM) {
                if (outPath != null && outPath.isFile() && outPath.exists()) {
                    Intent intent = new Intent();
                    intent.putExtra(Contants.TYPE, Contants.CAMERA);
                    intent.putExtra(CAMERAFILE, photoPath.getAbsolutePath());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            } else if(requestCode == Contants.ALBUM_CONTENT_DATA_CODE){
                ArrayList<String> object = (ArrayList<String>) data.getSerializableExtra(Contants.ALBUM_LIST);
                Intent intent = new Intent();
                intent.putExtra(Contants.TYPE, Contants.ALBUM);
                intent.putExtra(Contants.ALBUM_LIST, object);
                setResult(RESULT_OK, intent);
                finish();
            }
        }
    }

    @Override
    protected void onCameraPermissionsResultOK() {
        super.onCameraPermissionsResultOK();
        photoPath = startCamera(Contants.CAMERA_CODE_RESULT);
    }

}
